import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.spec.*;
import java.io.*;

public class DES
{
    DES() {}

    public byte[] Encrypt(SecretKey key, byte[] block) throws Exception {
		Cipher cipher = null;
		block = PadBlock(block, "Encrypt");
		
		try 
		{
			cipher = Cipher.getInstance("DES/ECB/NoPadding");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			return cipher.doFinal(block);
		} 
		catch (Exception e) { System.out.println(e.toString()); } 
		
        return null;
    }

    public byte[] Decrypt(SecretKey key, byte[] block) throws Exception {
		Cipher cipher = null;
		block = PadBlock(block, "Decrypt");
			
		try 
		{
			cipher = Cipher.getInstance("DES/ECB/NoPadding");
			cipher.init(Cipher.DECRYPT_MODE, key);
			return cipher.doFinal(block);
		} 
		catch (Exception e) { System.out.println(e.toString()); } 
		
        return null;
    }
	
	private byte[] PadBlock(byte[] block, String mode) throws Exception
	{
		if (block.length == 8) {}
		else if (block.length > 8)
			throw new Exception("DES " + mode + ": Block length > 8");
		else 
		{
			byte[] temp = new byte[8]; 
			
			for (int i = 0; i < block.length; i++)
				temp[i] = block[i];
				
			block = temp;	
		}
		
		return block;
	}
}




































